<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:p="http://primefaces.org/ui"
	xmlns:ui="http://java.sun.com/jsf/facelets">

<ui:composition template="template.xhtml">
	<ui:define name="corpo">

		<h2>Disciplinas</h2>

		<h:form id="cadForm">
			<p:growl id="growl" life="2500"></p:growl>
			<p:commandButton value="Novo cadastro" oncomplete="cadDialog.show()" />

			<p:dialog widgetVar="cadDialog" header="Cadastro Disciplina"
				closable="false" resizable="false" modal="true" hideEffect="slide"
				showEffect="clip" draggable="false">

				<h:panelGrid id="display" columns="3" cellpadding="4"
					style="margin:0 auto;">

					<h:outputLabel value="Nome: " />
					<p:inputText type="text" id="desc"
						value="#{disciplinaBean.disciplina.nome}"
						title="Insira aqui o nome da disciplina." />
					<p:message for="desc"></p:message>

					<h:outputLabel value="Carga horária: " />
					<p:inputText value="#{disciplinaBean.disciplina.cargaHoraria}"
						id="carg" label="Carga horária"
						title="Insira aqui a carga horária">
					</p:inputText>
					<p:message for="carg"></p:message>
					<h:outputText></h:outputText>
					<p:column>
						<p:commandButton id="btsalvar" value="Salvar"
							update="display :table:tabdisc growl"
							actionListener="#{disciplinaBean.grava}"
							oncomplete="handleCadastroRequest(xhr, status, args)">
						</p:commandButton>

						<p:commandButton value="Voltar"
							
							action="/disciplina.xhtml?faces-redurect=true" ajax="false"
							immediate="true"></p:commandButton>
					</p:column>
				</h:panelGrid>
			</p:dialog>

			<script type="text/javascript">
				function handleCadastroRequest(xhr, status, args) {
					if (args.validationFailed || !args.gravado) {
						jQuery('#cadDialog').effect("shake", {
							times : 3
						}, 100);
					} else {
						cadDialog.hide();
					}
				}
			</script>

		</h:form>
		<h:form id="table">

			<p:dataTable id="tabdisc" style="margin-top: 5px" var="disc"
				value="#{disciplinaBean.disciplinas}" paginator="true" rows="10"
				widgetVar="rend"
				rowStyleClass="#{empty rowIx or rowIx mod 2 ne 0 ? 'par' : 'impar'}"
				rowIndexVar="rowIx" emptyMessage="Nenhuma registro encontrado"
				filteredValue="#{disciplinaBean.disciplinasFiltradas}">

				<f:facet name="header">
					DISCIPLINAS
				</f:facet>

				<p:columnGroup type="header">
					<p:row>
						<p:column colspan="1" headerText="Disciplina"
							filterBy="#{disc.nome}" filterMatchMode="contains" />
						<p:column colspan="1" headerText="Carga horária"
							filterBy="#{disc.cargaHoraria}" filterMatchMode="contains" />
						<p:column colspan="2" headerText="Ações" />
					</p:row>
				</p:columnGroup>

				<p:column>
					#{disc.nome}
				</p:column>

				<p:column>
					<div align="center">#{disc.cargaHoraria}</div>
				</p:column>

				<p:column>
					<div align="center">
						<p:commandLink update=":discForm:display" title="Alterar"
							oncomplete="carDialog.show()">
							<f:setPropertyActionListener value="#{disc}"
								target="#{disciplinaBean.disciplina}" />
							<p:graphicImage value="resources/imagens/editar.png" width="24px"
								height="24px" />
						</p:commandLink>
					</div>
				</p:column>

				<p:column>
					<div align="center">
						<p:commandLink update=":discRemove:apaga :discRemove:apagDiscip"
							title="Remover" oncomplete="confirmDialog.show()"
							action="#{competenciaBean.preencheCompetenciasComDisciplinasSelecionada}">
							<f:setPropertyActionListener value="#{disc}"
								target="#{competenciaBean.disciplina}" />
							<f:setPropertyActionListener value="#{disc}"
								target="#{aulaBean.disciplinaSelecionada}" />
							<f:setPropertyActionListener value="#{disc}"
								target="#{disciplinaBean.disciplina}" />
							<p:graphicImage value="resources/imagens/delete.png" width="24px"
								height="24px" />
						</p:commandLink>
					</div>
				</p:column>
			</p:dataTable>

		</h:form>

		<h:form id="discForm">
			<p:dialog header="Alterar Disciplina" widgetVar="carDialog"
				resizable="false" id="carDlg" hideEffect="slide" showEffect="clip"
				closable="false" modal="true">

				<h:panelGrid id="display" columns="2" cellpadding="4"
					style="margin:0 auto;">

					<h:outputLabel value="Descrição: " />
					<p:inputText value="#{disciplinaBean.disciplina.nome}" id="altdesc"
						title="Insira aqui a descrição" />


					<h:outputLabel value="Carga horária: " />
					<p:inputText value="#{disciplinaBean.disciplina.cargaHoraria}"
						id="carg" label="Carga horária"
						title="Insira aqui a carga horária" />

					<p:commandButton value="Alterar"
						actionListener="#{disciplinaBean.atualiza}"
						oncomplete="handleAlteraRequest(xhr, status, args)"
						update="display :table:tabdisc :cadForm:growl" />

					<p:commandButton value="Cancelar" onclick="carDialog.hide()"
						action="#{disciplinaBean.limpar}" />

				</h:panelGrid>
			</p:dialog>

			<script type="text/javascript">
				function handleAlteraRequest(xhr, status, args) {
					if (args.validationFailed || !args.gravado) {
						jQuery('#carDialog').effect("shake", {
							times : 3
						}, 100);
					} else {
						carDialog.hide();
					}
				}
			</script>
		</h:form>
		<h:form id="discRemove">

			<p:dialog widgetVar="confirmDialog" resizable="false" id="apagDiscip"
				showEffect="fade" hideEffect="explode" closable="false" modal="true">

				<p:dataTable id="tableAulaSelec"
					value="#{aulaBean.aulasRelacionadasComDisciplina}" var="aula"
					rowStyleClass="#{empty rowIx or rowIx mod 2 ne 0 ? 'par' : 'impar'}"
					rowIndexVar="rowIx" emptyMessage="nenhuma aula relacionada"
					paginator="true" rows="6">
					<f:facet name="header">
					As seguintes aulas estão relacionados com essa disciplina. 
					
					</f:facet>
					<p:column headerText="Professor">
                    	#{aula.competencia.professor.usuario.nome} 
                 	</p:column>
					<p:column headerText="Matricula">
                    	#{aula.competencia.professor.usuario.matricula} 
                 	</p:column>
					<p:column headerText="Dia">
                    	#{aula.disponibilidade.dia.dia} 
                 	</p:column>
					<p:column headerText="Turno">
                    	#{aula.disponibilidade.turno} 
                 	</p:column>
					<p:column headerText="Horario">
                    	#{aula.disponibilidade.horario} 
                 	</p:column>
				</p:dataTable>

				<p:dataTable id="tablecomp" value="#{competenciaBean.competencias}"
					var="comp"
					rowStyleClass="#{empty rowIx or rowIx mod 2 ne 0 ? 'par' : 'impar'}"
					rowIndexVar="rowIx" emptyMessage="nenhum professor relacionado"
					paginator="true" rows="6">
					<f:facet name="header">
					Os seguintes professor estão relacionados com essa disciplina. 
					
					</f:facet>
					<p:column headerText="Nome">
                    	#{comp.professor.usuario.nome} 
                 	</p:column>
					<p:column headerText="Matricula">
                    	#{comp.professor.usuario.matricula} 
                 	</p:column>
					<f:facet name="footer">Têm certeza que deseja excluir?</f:facet>
				</p:dataTable>
				<h:panelGrid id="apaga" columns="2" cellpadding="4"
					style="margin:0 auto;">

					<p:commandButton value="Sim, remover"
						action="#{disciplinaBean.remove}" update=":table:tabdisc"
						onclick="confirmDialog.hide()" />

					<p:commandButton value="Não remover"
						action="#{disciplinaBean.limpar}" onclick="confirmDialog.hide()" />


				</h:panelGrid>
			</p:dialog>

		</h:form>
	</ui:define>
</ui:composition>
</html>